﻿//alert("menu"); //輝

/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* -------------------------- Start ------------------------------------- */
/* MU ----------------------- Menu -------------------------------------- */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/*
id="item121"
<script>
	var menuSelected = "item121";
</script>
*/


var menuSelected = null;
var navBar = "";  
var navBarHome = "<li><a href=\"javascript:void(0)\"><img id=\"homeNavIcon\" class=\"home\" src=\"../images/icon_home.gif\" /></a></li>";
var navBarArrow = "<li><img class=\"arrow\" src=\"../images/nav_arrow.gif\" /></li>";
/*
<div class="nav"> 
<img class="home" src="../images/icon_home.gif" /> 
<img class="arrow" src="../images/nav_arrow.gif" /> 
</div>
*/

var MU = {
	id : "MU",
	menus : new Array(), 
	disableAnimation : true,
	init : function(){
		//if(typeof CG != "undefined"){
			//CG.LT();
		//}		
		var _menus = CE.getElementsByClassName(document, "div", "topMenu", true);
		if(_menus.length > 0){ 
			for(var k=0; k<_menus.length; k++){	
				_menus[k].k = k;
				this.menus[k] = new MENU(_menus[k]);
				_menuSelected(_menus[k]);
			}		 
		}
	},		
	menuSelected : function(_menu, _itemId){
		function itemExist(_urls, _imId){
			var _selected = null;
			for(var i=0; i<_urls.length; i++){
				if(_urls[i].id == _imId){
					_selected = _urls[i];
					break;
				}
			}
			return _selected;	
		}
		function hlLevel(_url){
			//CE.fireClick(_url);
			//highlight all parent of selected item.
			//_url.className = _url.className + " hl";
			navBar = "<li>" + _url.innerHTML + "</li>" + navBarArrow + navBar;
			navBar = navBar.replace(/(<span.*span>)/, '');		
			navBar = navBar.replace(/(<SPAN.*SPAN>)/, '');				
		}
		var _URLs = CE.$s(_menu, "a");
		var selectedURL = itemExist(_URLs, _itemId);

		if(selectedURL != null){
			var _sParent = selectedURL.parentURL;
			//highlight selected item
			//selectedURL.className = selectedURL.className + " hl selected";	
			//construct nav status 
	
			navBar = "<li>" +  selectedURL.innerHTML + "</li>" + navBar;
			navBar = navBar.replace(/(<span.*span>)/, '');		
			navBar = navBar.replace(/(<SPAN.*SPAN>)/, '');					

			while(_sParent != null){
				hlLevel(_sParent);
				_sParent = _sParent.parentURL;
			}
		}
	}	
};

function _menuSelected(_menu){
	if(menuSelected != null){
		MU.menuSelected(_menu, menuSelected);	
		_navBarSelected();		
	}
}

function _navBarSelected(){
	var _nav = CE.$("nav");	
	if(_nav != null){
		_nav.innerHTML = "<ul>" + navBarHome + ((navBar !="") ? navBarArrow : "") + navBar + "</ul>";
		var _sytleNum = CE.$("menuCSS").href.split("/color_")[1].split("/")[0];
		//CE.$("homeNavIcon").title = _sytleNum;
		//STYLE.currentStyle = _sytleNum;
	}
}

MENU = function(_MU){
	var _div = _MU;
	var _tag = "ul";
	this.hasChild = function(_li){
		var _ULs = CE.$s(_li, _tag);
		return (_ULs.length > 0) ? true : false;
	};
	this.firstUL = function(base){
		var _ULs = CE.$s(base, _tag);
		return (_ULs.length > 0 ) ? _ULs[0] : null;
	};
	this.collapseLevel = function(e){
		var _thisObj = CE.eventObj(e);
		var _childUL = _thisObj.childUL;
		var _parentUL = CE.getParentByTagName(_thisObj, _tag);
		var _activeMu = MU.menus[_div.k];
		var _LIs = CE.LIs(_parentUL);
		for(var i=0; i<_LIs.length; i++){
			_url = _LIs[i].getElementsByTagName("a")[0];
//			_url.className = _url.className.replace("minus", "plus");
			var _UL = _activeMu.firstUL(_LIs[i]);
			if(_UL != null){				
				if(_UL.opened == true){
					//if(!MU.disableAnimation){
						//var _tweenCollapseH = Tween(_UL, "height", Strong.easeOut, 100, 1, 5, false);
						//_tweenCollapseH.addEventListener("MOTION_FINISH", _tweenCollapseCompleted);
					//}else{
						_tweenCollapseCompleted();
					//}
				}
			}
		}
		function _tweenCollapseCompleted(){
			//CE.alert("_tweenCollapseCompleted");
//			_UL.className = "hide";
			_UL.opened = false;
		}
	};
	this.parentOnClick = function(e){
		var thisObj = CE.eventObj(e);
		var _URL = thisObj;
		var childUL = thisObj.childUL;
		var _activeMu = MU.menus[_div.k];
		try{			
			if(_URL.opened){
				if(!MU.disableAnimation){
					var _tweenCloseH = Tween(childUL, "height", Strong.easeOut, 100, 1, 5, false);
					_tweenCloseH.addEventListener("MOTION_FINISH", _tweenCloseCompleted);
				}else{
					_tweenCloseCompleted();
				}
			}else{
				if(!MU.disableAnimation){
					var _tweenOpenH = Tween(childUL, "height", Strong.easeOut, 10, 100, 5, false);
					_tweenOpenH.addEventListener("MOTION_FINISH", _tweenOpensCompleted);
				}else{
					_tweenOpensCompleted();
				}
			}
		}catch(e){
			//alert(e);
		}
		function _tweenCloseCompleted(){
			//CE.alert("_tweenCompletedClose");
//			thisObj.className = thisObj.className.replace("minus", "plus");
//			childUL.className = "hide";
			_URL.opened = false;	
		}
		function _tweenOpensCompleted(){
			//CE.alert("_tweenOpensCompleted");
//			thisObj.className = thisObj.className.replace("plus", "minus");		
//			childUL.className = "show";
//			childUL.style.height = "";				
			_URL.opened = true;	
		}		
	};
	this.buildLevel = function(_LIs, _parentURL){
		for(var i=0; i<_LIs.length; i++){
			var _URL = CE.$s(_LIs[i], "a")[0];
//			_URL.title = _URL.id;						
			_URL.parentURL = _parentURL;
			var _hasChild = this.hasChild(_LIs[i]);
			if(_hasChild){			
				var _UL = this.firstUL(_LIs[i]);
				_URL.childUL = _UL;
				//_UL.parentURL = _URL;
				_URL.opened = false;
//				_URL.className = "plus";
				CE.addEventListener(_URL, "click", this.parentOnClick);	
				//_UL.style.border = "5px solid red";
				var __LIs = CE.LIs(_UL);
				this.buildLevel(__LIs, _URL);
			}else{
//				_URL.className = "arrow";						
			}
		}
	};
	_UL = this.firstUL(_div);
	_LIs = CE.LIs(_UL);
	this.buildLevel(_LIs, null);
};







/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/* -------------------------- End --------------------------------------- */
/* MU ----------------------- Menu -------------------------------------- */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */